package com.kangfu.api.controller;

import com.kangfu.api.interceptor.AuthorizationInterceptor;
import com.kangfu.api.service.INoticeService;
import com.kangfu.common.exception.BusinessException;
import com.kangfu.common.model.AjaxResult;
import com.kangfu.db.model.vo.NoticeVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/notice")
public class NoticeController {

    @Autowired
    public INoticeService noticeService;

    @GetMapping("/list")
    public AjaxResult list(@RequestAttribute(AuthorizationInterceptor.LOGIN_USER_ID) int userId, @RequestParam(name = "page", defaultValue = "1") int page) {
        List<NoticeVo> list = noticeService.selectNoticeByUserId(userId, page);
        return AjaxResult.success(list);
    }

    @GetMapping("/read")
    public AjaxResult read(@RequestAttribute(AuthorizationInterceptor.LOGIN_USER_ID) int userId, @RequestParam(name = "id", defaultValue = "0") int id) {
        if (id <= 0) {
            throw new BusinessException("参数错误");
        }
        noticeService.updateNoticeReadFlag(userId, id);
        return AjaxResult.success();
    }

    @GetMapping("/readAll")
    public AjaxResult readAll(@RequestAttribute(AuthorizationInterceptor.LOGIN_USER_ID) int userId) {
        noticeService.updateAllNoticeReadFlag(userId);
        return AjaxResult.success();
    }
}
